package com.finance3.springmvc.handler;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.finance3.springmvc.bean.User;
import com.finance3.springmvc.dao.queryUser;
import com.finance3.springmvc.dao.queryUserImple;


@Controller
public class springmvcHandler {
	
	/**
	 * 测试springmvc框架
	 * 2020-10-13_19:25
	 * @return
	 */
	@RequestMapping(value="/testspringmvc",method=RequestMethod.GET)
	public String testspringmvc() {
		return "success";
	}
	
	@RequestMapping(value="/query",method=RequestMethod.POST)
	public String toQueryResult(@RequestParam("uid")String id,
								@RequestParam("upw")String password,
								Map<String,Object> map) {
//		String id=(String) req.getParameter("uid");
//		String password=(String) req.getParameter("upw");
		queryUser queryuser = new queryUserImple();
		User user = queryuser.getUser(id,password);
//		req.setAttribute("user", user);
		map.put("user", user);
		String missMsg="0";
		if(user==null) {
			//missMsg="密码错误，请重新输入";
			missMsg="1";
//			req.setAttribute("missMsg", missMsg);
	
			/*
			 * 不知道为什么就是运行不了
			 * //下面两行是出来修改密码的 
			 * resp.setContentType("text/html;utf-8");
			 * resp.getWriter().println(missMsg);
			 */
			
//			req.setAttribute("uid", id);
//			req.getRequestDispatcher("query.jsp").forward(req, resp);
			map.put("missMsg", missMsg);
			map.put("uid",id);
			return "forward:query.jsp";
		}
		else {
//			req.getRequestDispatcher("queryResult.jsp").forward(req, resp);
			return "queryResult";
		}
//		return "success";
	}
	
	/**
	 * 效验用户账号是否存在
	 * 2020-10-14_10:56
	 * 晴天
	 * @param uid
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping(value="/checkUidServlet",method=RequestMethod.POST)
	public void checkUid(@RequestParam("uid")String uid,
						HttpServletResponse resp) throws IOException {
//		String uid = req.getParameter("uid");
		String msg="";
		if(uid!=null) {
			queryUser userid = new queryUserImple();
			String userByUid = userid.getUserByUid(uid);
			if(userByUid==null) {
				msg="1";
			}
			else {
				msg="0";
			}
		}
		else {
			msg="1";
		}
		resp.setContentType("text/html;utf-8");
		resp.getWriter().println(msg);
	}
	
	/**
	 * 效验修改密码页面用户原来密码是否正确
	 * @param id
	 * @param password
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping(value="/checkUpwServlet",method=RequestMethod.POST)
	public void chechUpw(@RequestParam("uid")String id,
						@RequestParam("upw")String password,
						HttpServletRequest req,
						HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
//		String id=(String) req.getParameter("uid");
//		String password=(String) req.getParameter("upw");
		
		//System.out.println("测试：checkupwservlet里面获取 到的uid和upw的值：\n"+id+"  "+password);
		
		queryUser queryuser = new queryUserImple();
		User user = queryuser.getUser(id,password);
		req.setAttribute("user", user);
		String missMsg="";
		if(user==null) {
			//missMsg="密码错误，请重新输入";
			missMsg="1";
			//req.setAttribute("missMsg", missMsg);
			
			//System.out.println("测试密码错误调用了checkUpwServlet了吗");
			
			//req.setAttribute("uid", id);
			//req.getRequestDispatcher("query.jsp").forward(req, resp);
		}
		else {
			missMsg="0";
			//System.out.println("测试密码正确调用了checkUpwServlet了吗");

		}
		resp.setContentType("text/html;utf-8");
	    resp.getWriter().println(missMsg);
	}
	
	@RequestMapping(value="/repassword",method=RequestMethod.POST)
	public String toAlertpassword(@RequestParam("uid")String uid,
								@RequestParam("password")String password,
								HttpServletRequest req,
								HttpServletResponse resp) {
//		String uid=req.getParameter("uid");
//		String password=req.getParameter("password");
		queryUser querydao = new queryUserImple();
		int i = querydao.alertUser(uid, password);
		String alertMsg="";
		if(i==1) {
			alertMsg="修改成功,继续修改或者去登录";
			//resp.sendRedirect("query.jsp");
		}
		else {
			alertMsg="抱歉，密码修改失败，继续修改";
			req.setAttribute("alertMsg", alertMsg);
//			req.getRequestDispatcher("repassword.jsp").forward(req, resp);
//			return "forward:repassword.jsp";
		}
		req.setAttribute("alertMsg", alertMsg);
		//req.setAttribute("u, o);
//		req.getRequestDispatcher("repassword.jsp").forward(req, resp);
		return "forward:repassword.jsp";
	}
	
}
